<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>贪吃蛇游戏</title>
    <style>
        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            margin: 0;
            background-color: #f0f0f0;
            font-family: Arial, sans-serif;
        }

        canvas {
            border: 2px solid #333;
            border-radius: 4px;
            background-color: white;
        }

        .instructions {
            margin-top: 20px;
            padding: 15px;
            background-color: white;
            border-radius: 4px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .instructions h2 {
            margin-top: 0;
            color: #333;
        }

        .instructions p {
            margin: 10px 0;
            color: #666;
        }

        button {
            margin-top: 10px;
            padding: 10px 20px;
            font-size: 16px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #45a049;
        }

        .back-button {
            margin-bottom: 20px;
            background-color: #2196F3;
        }

        .back-button:hover {
            background-color: #1976D2;
        }
    </style>
</head>
<body>
    <a href="/" class="back-button" style="text-decoration: none; color: white; padding: 10px 20px; border-radius: 4px; background-color: #2196F3;">← 返回首页</a>
    <canvas id="gameCanvas" width="600" height="400"></canvas>
    <button id="startButton">开始游戏</button>
    <div class="instructions">
        <h2>游戏说明</h2>
        <p>使用方向键控制蛇的移动方向</p>
        <p>吃到食物（红色方块）可以增加分数</p>
        <p>撞到墙壁或自己会导致游戏结束</p>
    </div>

    <script src="/static/snake.js"></script>
    <script>
        const canvas = document.getElementById('gameCanvas');
        const startButton = document.getElementById('startButton');
        let game = null;
        let gameLoop = null;

        function startGame() {
            if (gameLoop) {
                clearInterval(gameLoop);
            }
            game = new Snake(canvas);
            gameLoop = setInterval(() => {
                game.update();
                game.draw();
                if (game.gameOver) {
                    clearInterval(gameLoop);
                    startButton.textContent = '重新开始';
                }
            }, 100);
        }

        startButton.addEventListener('click', () => {
            startButton.textContent = '重新开始';
            startGame();
        });
    </script>
</body>
</html>